package net.asfun.jvalog.util;

import static net.asfun.jvalog.common.log.logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.vo.Setting;

public class ResponseUtil {

private static final Properties prop;
	
	static {
		prop = new Properties();
		try {
			prop.load(new FileInputStream(Setting.WEB_ROOT + "/WEB-INF/classes/interact.properties"));
		} catch (IOException e) {
			logger.log(Level.SEVERE, "properties read error", e.getCause());
		}
	}
	
	public static String feedback(String key, HttpServletRequest req, Map<String, Object> model) {
		if ( RequestUtil.isAjaxRequest(req) ) {
			model.put("type", "info");
			model.put("message", getMessage(key));
			return "json";
		} else {
			model.put("message", getMessage(key));
			model.put("forward", getForward(key));
			return "tip.html";
		}
	}
	
	public static final String getMessage(String key) {
		return prop.getProperty(key + ".message");
	}
	
	public static final String getForward(String key) {
		return prop.getProperty(key + ".forward");
	}
}
